﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the session entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   + Base
    ///    | - Flags
    ///    | - AccessedOn
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     | - Account(of the Primary level)
    ///     | - Source
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class SessionEntity : AuditableEntity
    {
        #region Fields

        private DateTime _accessedOn;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public SessionEntity()
        {
            _accessedOn = DateTime.UtcNow;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the global unique identifier.
        /// </summary>
        public virtual Guid Guid
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the account.
        /// </summary>
        public virtual AccountEntity Account
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the source.
        /// </summary>
        public virtual string Source
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual SessionFlags Flags
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the last acccess date.
        /// </summary>
        public virtual DateTime AccessedOn
        {
            get
            {
                return _accessedOn;
            }
            set
            {
                _accessedOn = value;
            }
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Keys)
            {
                obj["Guid"] = Guid;
                obj["Account"] = Account.Disclose(DisclosureLevel.Primary, options);
                obj["Source"] = Source;
            }
            if(level >= DisclosureLevel.Base)
            {
                obj["Flags"] = Flags.ToString();
                obj["AccessedOn"] = AccessedOn;
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapper.
    /// </summary>
    public class SessionMapping : EntityMapping<SessionEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public SessionMapping()
        {
            Table("fwk_Sessions");
            Id(x => x.EntityID, m =>
            {
                m.Column("SessionID");
                m.Generator(Generators.Identity);
            });
            Property(x => x.Guid, m =>
            {
                m.Unique(true);
                m.Index("IX_fwk_Sessions_Guid");
                m.NotNullable(true);
            });
            ManyToOne(x => x.Account, m =>
            {
                m.Column("AccountID");
                m.ForeignKey("FK_fwk_Sessions_fwk_Accounts");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            Property(x => x.Source, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.AccessedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Cache(m =>
            {
                m.Usage(CacheUsage.ReadWrite);
            });
        }
    }
}
